<style lang="scss" scoped>
	.nav {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		background-color: #f5f5f5;
		z-index: 9999;
	}

	.top {
		// padding: 20rpx 0;
		width: 100%;
	}

	.search {
		width: 690rpx;
		height: 68rpx;
		background: #f4f5f7;
		border: 2rpx solid #f5f5f5;
		border-radius: 46rpx;
		margin: 0rpx auto;
		padding: 20rpx;

		.ipt {
			flex: 1;
		}
	}

	.tab {
		margin-top: 20rpx;
		padding: 0 20rpx;
		height: 100rpx;
		border-bottom: 1px solid #ccc;

		.actived {
			color: #409EFF;
		}
	}

	.lists {
		// margin-top: 228rpx;
		margin-top: 110rpx;

		.list {
			height: 100rpx;
			padding: 20rpx;
		}

		.actived {
			color: #409EFF;
		}
	}

	.footer {
		width: 100%;
		background-color: white;
		position: fixed;
		bottom: 0;
		left: 0;
		border-top: 1px solid #cccccc;
		z-index: 999;
	}
</style>
<template>
	<view class="content">
		<view class="nav">

			<view class="tab align-flex bgwhite">
				<view class="gray m-right20">已选人员</view>
				<view style="max-width: 550rpx;" class="textColor one" @click="change(index)"> <span v-for="item,index in choose" :key="index"
						class="actived">{{item.name}}<span v-if="index<choose.length-1"
							class="p-left10 p-right10">/</span></span> </view>
			</view>
		</view>
		<view class="lists bgwhite">
			<mix-tree :list="list" :type="type" :params="treeParams" @treeItemClick="treeItemClick"></mix-tree>
			<!-- <view class="list between-flex f28" v-for="item,index in list[current]" :key="index"
				@click="choose(item,index)">
				<view class="textColor" :class="index==chooseIndex[current]?'actived':''">{{item.dept_name}}</view>
				<view v-if="item.children">
					<u-icon name="arrow-right"></u-icon>
				</view>
				<view v-else>
					<u-icon v-if="chooseIndex[current]==index" color="#409EFF" name="checkbox-mark"></u-icon>
				</view>
			</view> -->

		</view>
		<view class="space"></view>
		<view class="footer center-flex paddingBottom">
			<view class="lbtn" @click="confirm">确定</view>
		</view>
	</view>
</template>

<script>
	import mixTree from '@/components/mix-tree/mix-tree';
	export default {
		components: {
			mixTree
		},
		data() {
			return {
				treeParams: {
					defaultIcon: '/static/i2.png', // 默认图标
					currentIcon: '/static/i1.png', // 展开图标
					lastIcon: '/static/i3.png', //最后一级图标
					border: false // 边框， 默认不显示
				},
				list: [], //显示的列表
				choose: [],
				type:1,//1多选 2单选
				users:[]

			}
		},
		onLoad(e) {
			if(e.hasOwnProperty('type')){
				this.type=e.type
			}
			if(e.hasOwnProperty('users')){
				this.users=e.users.split(',')
				console.log(this.users)
			}
			this.getlist()
		},
		methods: {
			treeItemClick(item, list) {
				console.log(item,this.choose)
				var flag=false
				var list=this.choose
				var index=0
				list.forEach((item2,i)=>{
					if(item2.userId==item.userId){
						flag=true
						index=i
					}
				})
				console.log(index)
				if(item.checked){
					
					if(!flag){
						if(this.type==2||this.type==3){
							this.choose=[] //若为单选，每次只放一个
						}
						this.choose.unshift(item)
						
					}
				}else{
					if(flag){
						
						this.choose.splice(index,1)
					}
				}
			},
			//点击最后一级时触发该事件
			confirm() {
				console.log(this.choose)
				var list = this.choose

				var name = [],
					value = []
				list.forEach(item => {
					name.push(item.name)
					value.push(item.userId)

				})
			
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				if(this.type==1){
					if (prevPage.$vm.hasOwnProperty('person')) {
					
						prevPage.$vm.person = {
							name: name.join('/'),
							value:value.join(','),
							id:value.join(',')
						}
					}
				}else if(this.type==2){
					if (prevPage.$vm.hasOwnProperty('people')) {
					
						prevPage.$vm.people = {
							name: name.join('/'),
							value:value.join(','),
							id:value.join(',')
						}
					}
				}else if(this.type==3){
					if (prevPage.$vm.hasOwnProperty('principal')) {
					
						prevPage.$vm.principal = {
							name: name.join('/'),
							value:value.join(','),
							id:value.join(',')
						}
					}
					uni.$emit('choosePerson2',{
								name: name.join('/'),
								value:value.join(','),
								id:value.join(',')
							})
				}
				uni.$emit('choosePerson',{
							name: name.join('/'),
							value:value.join(','),
							id:value.join(',')
						})
				
				uni.navigateBack({

				})
			},
			getlist() {

				var list = uni.getStorageSync('team')
				
				list.forEach((item, index) => {
					item.id = index + 1
					item.children.forEach((item2,index2)=>{
						if(!item2.hasOwnProperty('userId')){
							item.children.splice(index2,1)
						}
					})
				})
				// console.log(list)
				list.forEach((item, index) => {
					
					item.children.forEach((item2,index2)=>{
						// console.log(item2.name,(item2.userId))
						if(this.users.indexOf(String(item2.userId))!=-1){
							let flag=true
							this.choose.forEach(user=>{
								if(user.userId==item2.userId){
									flag=false
								}
							})
							if(flag){
								this.choose.push(item2)
								
								item2.checked=true
							}
							
						}else{
							item2.checked=false
						}
						
					})
				})
				 // console.log(list)
				this.list = list
			
			}


		}
	}
</script>
